using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Scurvy.Media
{

    public abstract class Video
    {
        #region Fields
        private int _numberOfFrames;
        private double _frameRate;
        private double _videoLength;
        private int _currentFrame;
        private int _frameToDraw;
        private bool _playing;
        private DateTime _started;

        /// <summary>Whether or not to play continuously when the last frame is reached.</summary>
        public bool Loop = true;
        #endregion

        #region Properties

        public double FrameRate
        {
            get { return _frameRate; }
            //set { _frameRate = value; }
        }

        public int CurrentIndex
        {
            get { return _frameToDraw; }

        }

        public int Count
        {
            get { return _numberOfFrames - 1; }
        }

        public bool IsPlaying
        {
            get { return _playing; }
        }

        public abstract Texture2D CurrentTexture { get;}

        #endregion

        internal Video(int numberOfFrames, double frameRate)
        {
            _frameRate = frameRate;
            _videoLength = numberOfFrames / _frameRate;
            _numberOfFrames = numberOfFrames;
            System.Diagnostics.Debug.WriteLine(_frameRate.ToString() + " " + _videoLength.ToString());
        }

        #region Public Methods
        public void Play()
        {
            _started = DateTime.Now;
            _playing = true;
            System.Diagnostics.Debug.WriteLine("Starting to play", _started.ToString());
        }

        public void Stop()
        {
            _playing = false;
        }

        /// <summary>
        /// Advance the frame based on time passed
        /// </summary>
        public void Update()
        {
            if (_playing)
            {
                double secondsElapsed = ((TimeSpan)(DateTime.Now - _started)).TotalSeconds;
                double leftToPlay = _videoLength - secondsElapsed;
                double percentageDone = 1 - (leftToPlay / _videoLength);
                int frame = (int)Math.Round(this.Count * percentageDone);

                System.Diagnostics.Debug.WriteLine("Playing:\n\tsecondsElapsed:" + secondsElapsed.ToString() + "\n\tleftToPlay:" + percentageDone.ToString() + "\n\tframe:" + frame.ToString());

                _frameToDraw = _currentFrame;
                int lastFrame = this.Count - 1;

                if (_frameToDraw > lastFrame)
                {
                    _frameToDraw = 0;
                }
                _currentFrame = frame;

                if (_currentFrame > lastFrame)
                {
                    _currentFrame = 0;
                    if (this.Loop)
                    {
                        _started = DateTime.Now;
                    }
                    else
                    {
                        _playing = false;
                    }
                }
                else
                {
                    _currentFrame++;
                }
            }
        }
        #endregion
    }
}
